using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace WIT.Global;

/// <summary>
/// Web客户端帮助类
/// </summary>
public class WebClientHelper
{
	/// <summary>
	/// 客户端IP地址
	/// </summary>
	public static string IP => HttpContext.Current.Request.UserHostAddress;

	/// <summary>
	/// 客户端浏览器类型
	/// </summary>
	public static string BrowserName => HttpContext.Current.Request.Browser.Browser;

	/// <summary>
	/// 客户端浏览器版本号
	/// </summary>
	public static string BrowserVersion => HttpContext.Current.Request.Browser.Version;

	/// <summary>
	/// 客户端操作系统
	/// </summary>
	public static string OS
	{
		get
		{
			string userAgent = HttpContext.Current.Request.UserAgent;
			string result = "unknown";
			if (userAgent.Contains("NT 10.0"))
			{
				result = "Windows 10";
			}
			else if (userAgent.Contains("NT 6.2"))
			{
				result = "Windows 8";
			}
			else if (userAgent.Contains("NT 6.1"))
			{
				result = "Windows 7";
			}
			else if (userAgent.Contains("NT 6.0"))
			{
				result = "Windows Vista/Server 2008";
			}
			else if (userAgent.Contains("NT 5.2"))
			{
				result = "Windows Server 2003";
			}
			else if (userAgent.Contains("NT 5.1"))
			{
				result = "Windows XP";
			}
			else if (userAgent.Contains("NT 5"))
			{
				result = "Windows 2000";
			}
			else if (userAgent.Contains("NT 4"))
			{
				result = "Windows NT4";
			}
			else if (userAgent.Contains("Me"))
			{
				result = "Windows Me";
			}
			else if (userAgent.Contains("98"))
			{
				result = "Windows 98";
			}
			else if (userAgent.Contains("95"))
			{
				result = "Windows 95";
			}
			else if (userAgent.Contains("Mac"))
			{
				result = "Mac";
			}
			else if (userAgent.Contains("Unix"))
			{
				result = "UNIX";
			}
			else if (userAgent.Contains("Linux"))
			{
				result = "Linux";
			}
			else if (userAgent.Contains("SunOS"))
			{
				result = "SunOS";
			}
			return result;
		}
	}

	/// <summary>  
	/// 获取公网IP  
	/// </summary>  
	/// <returns></returns>  
	public static string GetNetIP()
	{
		string text = "";
		try
		{
			Stream responseStream = WebRequest.Create("http://city.ip138.com/ip2city.asp").GetResponse().GetResponseStream();
			StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
			string text2 = streamReader.ReadToEnd();
			int num = text2.IndexOf("[") + 1;
			int num2 = text2.IndexOf("]", num);
			text = text2.Substring(num, num2 - num);
			streamReader.Close();
			responseStream.Close();
			return text;
		}
		catch
		{
			if (Dns.GetHostEntry(Dns.GetHostName()).AddressList.Length > 1)
			{
				text = Dns.GetHostEntry(Dns.GetHostName()).AddressList[1].ToString();
			}
			if (string.IsNullOrEmpty(text))
			{
				return IP;
			}
			return text;
		}
	}
}
